package com.xjrsoft.module.erpModel.caseErpDevice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.enums.YesOrNoEnum;
import com.xjrsoft.common.exception.MyException;
import com.xjrsoft.common.utils.RedisUtil;
import com.xjrsoft.module.erpModel.caseErpDevice.dto.AddCaseErpDeviceInspectDto;
import com.xjrsoft.module.erpModel.caseErpDevice.dto.UpdateCaseErpDeviceInspectDto;
import com.xjrsoft.module.erpModel.caseErpDevice.entity.CaseErpDeviceInfo;
import com.xjrsoft.module.erpModel.caseErpDevice.entity.CaseErpDeviceInspect;
import com.xjrsoft.module.erpModel.caseErpDevice.mapper.CaseErpDeviceInspectMapper;
import com.xjrsoft.module.erpModel.caseErpDevice.service.ICaseErpDeviceInfoService;
import com.xjrsoft.module.erpModel.caseErpDevice.service.ICaseErpDeviceInspectService;
import com.xjrsoft.module.organization.entity.User;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 运维巡检【case_erp_device_inspect】 服务实现类
 * </p>
 *
 * @author hnyyzy
 * @since 2023-07-10
 */
@Service
@AllArgsConstructor
public class CaseErpDeviceInspectServiceImpl extends MPJBaseServiceImpl<CaseErpDeviceInspectMapper, CaseErpDeviceInspect> implements ICaseErpDeviceInspectService {
    private final ICaseErpDeviceInfoService caseErpDeviceInfoService;

    private final RedisUtil redisUtil;

    @Override
    public Boolean add(AddCaseErpDeviceInspectDto dto) {
        CaseErpDeviceInspect caseErpDeviceInspect = BeanUtil.toBean(dto, CaseErpDeviceInspect.class);
        //根据设备名称获取设备信息
        CaseErpDeviceInfo caseErpDeviceInfo = caseErpDeviceInfoService.getOne(Wrappers.<CaseErpDeviceInfo>query().lambda().eq(CaseErpDeviceInfo::getId, dto.getDeviceId()));
        if (caseErpDeviceInfo == null){
            throw new MyException("找不到此设备！");
        }
        if(dto.getState().equals(YesOrNoEnum.NO.getCode())){
            //更新设备信息表状态
            caseErpDeviceInfo.setState(YesOrNoEnum.NO.getCode());
            caseErpDeviceInfoService.updateById(caseErpDeviceInfo);
        }
        caseErpDeviceInspect.setDeviceId(caseErpDeviceInfo.getId());
        caseErpDeviceInspect.setName(caseErpDeviceInfo.getName());
        caseErpDeviceInspect.setCode(caseErpDeviceInfo.getCode());
        caseErpDeviceInspect.setModel(caseErpDeviceInfo.getModel());
        caseErpDeviceInspect.setAddress(caseErpDeviceInfo.getAddress());
        List<User> allUser = redisUtil.get(GlobalConstant.USER_CACHE_KEY, new TypeReference<List<User>>() {
        });
        String allUserIdStr = StrUtil.join(StringPool.COMMA, dto.getCheckedUserIds());
        List<Long> ids = Arrays.stream(allUserIdStr.split(StringPool.COMMA)).map(Convert::toLong).collect(Collectors.toList());
        List<String> names = allUser.stream().filter(x -> ids.contains(x.getId())).map(User::getName).collect(Collectors.toList());
        caseErpDeviceInspect.setCheckedUserNames(StrUtil.join(StringPool.COMMA,names));
        this.save(caseErpDeviceInspect);
        return true;
    }

    @Override
    public Boolean update(UpdateCaseErpDeviceInspectDto dto) {
        CaseErpDeviceInspect caseErpDeviceInspect = BeanUtil.toBean(dto, CaseErpDeviceInspect.class);
        //根据设备名称获取设备信息
        CaseErpDeviceInfo caseErpDeviceInfo = caseErpDeviceInfoService.getOne(Wrappers.<CaseErpDeviceInfo>query().lambda().eq(CaseErpDeviceInfo::getId, dto.getDeviceId()));
        if (caseErpDeviceInfo == null){
            throw new MyException("找不到此设备！");
        }
        if(dto.getState().equals(YesOrNoEnum.NO.getCode())){
            //更新设备信息表状态
            caseErpDeviceInfo.setState(YesOrNoEnum.NO.getCode());
            caseErpDeviceInfoService.updateById(caseErpDeviceInfo);
        }
        caseErpDeviceInspect.setDeviceId(caseErpDeviceInfo.getId());
        caseErpDeviceInspect.setName(caseErpDeviceInfo.getName());
        caseErpDeviceInspect.setCode(caseErpDeviceInfo.getCode());
        caseErpDeviceInspect.setModel(caseErpDeviceInfo.getModel());
        caseErpDeviceInspect.setAddress(caseErpDeviceInfo.getAddress());
        List<User> allUser = redisUtil.get(GlobalConstant.USER_CACHE_KEY, new TypeReference<List<User>>() {
        });
        String allUserIdStr = StrUtil.join(StringPool.COMMA, dto.getCheckedUserIds());
        List<Long> ids = Arrays.stream(allUserIdStr.split(StringPool.COMMA)).map(Convert::toLong).collect(Collectors.toList());
        List<String> names = allUser.stream().filter(x -> ids.contains(x.getId())).map(User::getName).collect(Collectors.toList());
        caseErpDeviceInspect.setCheckedUserNames(StrUtil.join(StringPool.COMMA,names));
        this.updateById(caseErpDeviceInspect);
        return true;
    }

}
